using System;
namespace MicroMsgBackground.Protocol
{
	public class TLVPack
	{
		public class TLVPackHeader
		{
			public byte[] h_pcBuf;
			public int offset;
			public byte[] getBuf()
			{
				return this.h_pcBuf;
			}
			public void setBuf(byte[] byte_0)
			{
				this.h_pcBuf = byte_0;
			}
			public byte getMagic()
			{
				return this.h_pcBuf[this.offset];
			}
			public TLVPack.TLVPackHeader setMagic(byte byte_0)
			{
				this.h_pcBuf[this.offset] = byte_0;
				return this;
			}
			public byte getMode()
			{
				return this.h_pcBuf[this.offset + 1];
			}
			public TLVPack.TLVPackHeader setMode(byte byte_0)
			{
				this.h_pcBuf[this.offset + 1] = byte_0;
				return this;
			}
			public byte[] getCheckSum()
			{
				return new byte[]
				{
					this.h_pcBuf[this.offset + 2],
					this.h_pcBuf[this.offset + 3]
				};
			}
			public TLVPack.TLVPackHeader setCheckSum(byte[] byte_0)
			{
				this.h_pcBuf[this.offset + 2] = byte_0[0];
				this.h_pcBuf[this.offset + 3] = byte_0[1];
				return this;
			}
			public int getUsedSize()
			{
				return (int)(this.h_pcBuf[this.offset + 4] & 255) | (int)(this.h_pcBuf[this.offset + 5] & 255) << 8 | (int)(this.h_pcBuf[this.offset + 6] & 255) << 16 | (int)(this.h_pcBuf[this.offset + 7] & 255) << 24;
			}
			public TLVPack.TLVPackHeader setUsedSize(int int_0)
			{
				this.h_pcBuf[this.offset + 4] = (byte)(int_0 & 255);
				this.h_pcBuf[this.offset + 5] = (byte)(int_0 >> 8 & 255);
				this.h_pcBuf[this.offset + 6] = (byte)(int_0 >> 16 & 255);
				this.h_pcBuf[this.offset + 7] = (byte)(int_0 >> 24 & 255);
				return this;
			}
			public int getSize()
			{
				return 12;
			}
		}
		public abstract class TLVBody
		{
			public int iType;
			public int iLength;
			public byte[] pcValPtr;
			public int pcValPtrOffset;
			public int iNextOffset;
			public TLVBody()
			{
				this.iType = 0;
				this.iLength = 0;
				this.pcValPtr = null;
				this.pcValPtrOffset = 0;
				this.iNextOffset = 0;
			}
			public abstract bool MapTo(byte[] apcSrc, int aiSrcSize, int aiOffset);
		}
		public class FixedSizeTLVBody : TLVPack.TLVBody
		{
			public override bool MapTo(byte[] apcSrc, int aiSrcSize, int aiOffset)
			{
				if (apcSrc == null || aiSrcSize <= 0 || aiOffset < 0 || aiOffset >= aiSrcSize)
				{
					return false;
				}
				int iType = TLVUtil.byte2int(apcSrc, aiOffset, 4, true);
				int num = TLVUtil.byte2int(apcSrc, aiOffset + 4, 4, true);
				this.pcValPtr = apcSrc;
				this.pcValPtrOffset = aiOffset + 4 + 4;
				int num2 = aiOffset + 4 + 4 + num;
				if (num < 0)
				{
					return false;
				}
				if (this.pcValPtrOffset > aiSrcSize)
				{
					return false;
				}
				if (num2 > aiSrcSize)
				{
					return false;
				}
				if (num2 == aiSrcSize)
				{
					this.iNextOffset = 0;
				}
				else
				{
					this.iNextOffset = num2;
				}
				this.iType = iType;
				this.iLength = num;
				this.pcValPtr = apcSrc;
				return true;
			}
		}
		public class tVariableSizeTLVItem : TLVPack.TLVBody
		{
			public override bool MapTo(byte[] apcSrc, int aiSrcSize, int aiOffset)
			{
				if (apcSrc == null || aiSrcSize <= 0 || aiOffset < 0 || aiOffset >= aiSrcSize)
				{
					return false;
				}
				int num = 0;
				int num2 = TLVUtil.DecodeVByte32(ref num, apcSrc, aiOffset);
				int iType = num;
				int num3 = TLVUtil.DecodeVByte32(ref num, apcSrc, aiOffset + num2);
				int num4 = num;
				this.pcValPtrOffset = aiOffset + num2 + num3;
				int num5 = aiOffset + num2 + num3 + num4;
				if (num4 < 0)
				{
					return false;
				}
				if (this.pcValPtrOffset > aiSrcSize)
				{
					return false;
				}
				if (num5 > aiSrcSize)
				{
					return false;
				}
				if (num5 == aiSrcSize)
				{
					this.iNextOffset = 0;
				}
				else
				{
					this.iNextOffset = num5;
				}
				this.iType = iType;
				this.iLength = num4;
				this.pcValPtr = apcSrc;
				return true;
			}
		}
		public const int c_iSizeofType = 4;
		public const int c_iSizeofLength = 4;
		public const int c_iInitializeSize = 1024;
		public const int c_iBufGrowUnit = 1024;
		public const int TLVMAGIC = 129;
		private byte[] m_pcBuf;
		private int m_iAllocSize;
		private int m_iUsedSize;
		private TLVPack.TLVPackHeader m_ptHeader;
		public int m_eDefaultMode;
		public TLVPack(int aiInitBufSize, int mode)
		{
			this.m_eDefaultMode = mode;
			this.m_pcBuf = null;
			this.m_iAllocSize = 0;
			this.m_iUsedSize = 0;
			this.m_ptHeader = new TLVPack.TLVPackHeader();
		}
		public byte[] getBuf()
		{
			return this.m_pcBuf;
		}
		public int getAllocSize()
		{
			return this.m_iAllocSize;
		}
		public int getUsedSize()
		{
			return this.m_iUsedSize;
		}
		private void clear()
		{
			if (this.m_pcBuf == null)
			{
				this.m_iAllocSize = 0;
				this.m_iUsedSize = 0;
				this.m_ptHeader = null;
				return;
			}
			if (this.m_iAllocSize < this.m_ptHeader.getSize())
			{
				this.m_ptHeader = null;
				this.m_iUsedSize = 0;
				return;
			}
			TLVUtil.fillByteArray(this.m_pcBuf, 0);
			this.m_iUsedSize = this.m_ptHeader.getSize();
			this.m_ptHeader.h_pcBuf = this.m_pcBuf;
			this.m_ptHeader.offset = 0;
			TLVPack.TLVPackHeader arg_92_0 = this.m_ptHeader.setMagic(129);
			byte[] checkSum = new byte[2];
			arg_92_0.setCheckSum(checkSum).setUsedSize(0).setMode((byte)this.m_eDefaultMode);
		}
		public bool isValidTLVPack(byte[] apcBuf, int aiUsedSize, int offset)
		{
			if (apcBuf == null)
			{
				return false;
			}
			if (aiUsedSize < this.m_ptHeader.getSize())
			{
				return false;
			}
			TLVPack.TLVPackHeader tLVPackHeader = new TLVPack.TLVPackHeader();
			tLVPackHeader.h_pcBuf = apcBuf;
			tLVPackHeader.offset = offset;
			if (tLVPackHeader.getMagic() != 129)
			{
				return false;
			}
			int num = aiUsedSize - tLVPackHeader.getSize();
			int usedSize = tLVPackHeader.getUsedSize();
			if (usedSize != num)
			{
				return false;
			}
			byte[] checkSum = TLVUtil.getCheckSum(apcBuf, offset + tLVPackHeader.getSize(), num);
			byte[] checkSum2 = tLVPackHeader.getCheckSum();
			if (checkSum[0] == checkSum2[0])
			{
				if (checkSum[1] == checkSum2[1])
				{
					if (num == 0)
					{
						return true;
					}
					TLVPack.FixedSizeTLVBody fixedSizeTLVBody = new TLVPack.FixedSizeTLVBody();
					TLVPack.tVariableSizeTLVItem tVariableSizeTLVItem = new TLVPack.tVariableSizeTLVItem();
					TLVPack.TLVBody tLVBody;
					if (tLVPackHeader.getMode() == 0)
					{
						tLVBody = fixedSizeTLVBody;
					}
					else
					{
						if (tLVPackHeader.getMode() != 1)
						{
							return false;
						}
						tLVBody = tVariableSizeTLVItem;
					}
					if (!tLVBody.MapTo(apcBuf, offset + aiUsedSize, offset + tLVPackHeader.getSize()))
					{
						return false;
					}
					while (tLVBody.iNextOffset != 0)
					{
						if (!tLVBody.MapTo(apcBuf, offset + aiUsedSize, tLVBody.iNextOffset))
						{
							return false;
						}
					}
					return true;
				}
			}
			return false;
		}
		public int Attach(byte[] apcBuf, int aiUsedSize, int aiAllocSize)
		{
			if (apcBuf == null || aiUsedSize == 0)
			{
				return -4;
			}
			if (aiAllocSize != 0 && aiAllocSize < aiUsedSize)
			{
				return -4;
			}
			if (!this.isValidTLVPack(apcBuf, aiUsedSize, 0))
			{
				return -5;
			}
			if (this.m_pcBuf != null)
			{
				this.m_pcBuf = null;
			}
			this.m_pcBuf = apcBuf;
			this.m_ptHeader.h_pcBuf = this.m_pcBuf;
			this.m_iAllocSize = ((aiAllocSize == 0) ? aiUsedSize : aiAllocSize);
			this.m_iUsedSize = aiUsedSize;
			this.m_eDefaultMode = (int)this.m_ptHeader.getMode();
			return 0;
		}
		public int Detach(ref byte[] appcBufFather, ref int apiUsedSizeFather, ref int apiAllocSizeFather)
		{
			if (appcBufFather == null && apiUsedSizeFather == 0 && apiAllocSizeFather == 0)
			{
				this.m_pcBuf = null;
				this.clear();
				return 0;
			}
			if (appcBufFather == null)
			{
				return -4;
			}
			appcBufFather = null;
			apiUsedSizeFather = 0;
			apiAllocSizeFather = 0;
			appcBufFather = this.m_pcBuf;
			apiUsedSizeFather = this.m_iUsedSize;
			apiAllocSizeFather = this.m_iAllocSize;
			byte[] checkSum = TLVUtil.getCheckSum(this.m_pcBuf, this.m_ptHeader.getSize(), this.m_iUsedSize - this.m_ptHeader.getSize());
			this.m_ptHeader.setCheckSum(checkSum);
			int usedSize = this.m_iUsedSize - this.m_ptHeader.getSize();
			this.m_ptHeader.setUsedSize(usedSize);
			this.m_pcBuf = null;
			this.clear();
			return 0;
		}
		public int CopyTo(byte[] apcBuf, int offset, ref int apiSize)
		{
			if (apcBuf == null || apiSize == 0)
			{
				return -4;
			}
			int num = apiSize;
			if (num < this.m_iUsedSize)
			{
				apiSize = this.m_iUsedSize;
				return -7;
			}
			byte[] checkSum = TLVUtil.getCheckSum(this.m_pcBuf, this.m_ptHeader.getSize(), this.m_iUsedSize - this.m_ptHeader.getSize());
			this.m_ptHeader.setCheckSum(checkSum);
			int usedSize = this.m_iUsedSize - this.m_ptHeader.getSize();
			this.m_ptHeader.setUsedSize(usedSize);
			if (this.m_iUsedSize > 0)
			{
				Buffer.BlockCopy(this.m_pcBuf, 0, apcBuf, offset, this.m_iUsedSize);
			}
			apiSize = this.m_iUsedSize;
			return 0;
		}
		public int CopyFrom(byte[] apcBuf, int aiUsedSize, int offset)
		{
			return this.CopyFrom(apcBuf, aiUsedSize, offset, 0);
		}
		public int CopyFrom(byte[] apcBuf, int aiUsedSize, int offset, int aiAllocSize)
		{
			if ((apcBuf == null && aiUsedSize < this.m_ptHeader.getSize()) || (aiAllocSize != 0 && aiAllocSize < aiUsedSize) || aiUsedSize < 0)
			{
				return -4;
			}
			if (!this.isValidTLVPack(apcBuf, aiUsedSize, offset))
			{
				return -5;
			}
			if (this.m_pcBuf == null || this.m_pcBuf.Length < aiUsedSize)
			{
				byte[] array = new byte[aiUsedSize];
				if (array == null)
				{
					return -1;
				}
				this.m_pcBuf = array;
			}
			Buffer.BlockCopy(apcBuf, offset, this.m_pcBuf, 0, aiUsedSize);
			this.m_ptHeader.h_pcBuf = this.m_pcBuf;
			this.m_iAllocSize = ((aiAllocSize == 0) ? aiUsedSize : aiAllocSize);
			this.m_iUsedSize = aiUsedSize;
			this.m_eDefaultMode = (int)this.m_ptHeader.getMode();
			return 0;
		}
		private int ensureSpace(int aiType, int aiReqSize)
		{
			int num;
			if (this.m_eDefaultMode == 0)
			{
				num = aiReqSize + 4 + 4;
			}
			else
			{
				if (this.m_eDefaultMode != 1)
				{
					return -8;
				}
				int num2 = 0;
				byte[] apcBuffer = new byte[6];
				int num3 = TLVUtil.DecodeVByte32(ref num2, apcBuffer, 0);
				int num4 = TLVUtil.DecodeVByte32(ref num2, apcBuffer, 0);
				num = num3 + num4 + aiReqSize;
			}
			if (this.m_pcBuf == null || this.m_iUsedSize <= 0)
			{
				num += this.m_ptHeader.getSize();
			}
			if (num >= 0 && num + this.m_iUsedSize >= 0)
			{
				if (num > this.m_iAllocSize - this.m_iUsedSize)
				{
					int i;
					for (i = this.m_iAllocSize + 1024; i < num + this.m_iUsedSize; i *= 2)
					{
					}
					if (i < 0)
					{
						i = num + this.m_iUsedSize;
					}
					byte[] array = new byte[i];
					bool flag = false;
					if (this.m_pcBuf == null && this.m_iUsedSize <= 0)
					{
						flag = true;
					}
					else
					{
						Buffer.BlockCopy(this.m_pcBuf, 0, array, 0, this.m_iUsedSize);
					}
					this.m_iAllocSize = i;
					this.m_pcBuf = array;
					this.m_ptHeader.h_pcBuf = this.m_pcBuf;
					if (flag)
					{
						this.clear();
					}
				}
				return 0;
			}
			return -3;
		}
		public int addByteArray(int aiType, byte[] aoVal, int size)
		{
			if (0 > this.ensureSpace(aiType, size))
			{
				return -2;
			}
			if (this.m_eDefaultMode == 0)
			{
				Buffer.BlockCopy(TLVUtil.int2byte(aiType, 4, true), 0, this.m_pcBuf, this.m_iUsedSize, 4);
				this.m_iUsedSize += 4;
				Buffer.BlockCopy(TLVUtil.int2byte(size, 4, true), 0, this.m_pcBuf, this.m_iUsedSize, 4);
				this.m_iUsedSize += 4;
			}
			else
			{
				if (this.m_eDefaultMode != 1)
				{
					return -8;
				}
				int num = TLVUtil.EncodeVByte32(aiType, this.m_pcBuf, this.m_iUsedSize);
				this.m_iUsedSize += num;
				int num2 = TLVUtil.EncodeVByte32(size, this.m_pcBuf, this.m_iUsedSize);
				this.m_iUsedSize += num2;
			}
			Buffer.BlockCopy(aoVal, 0, this.m_pcBuf, this.m_iUsedSize, size);
			this.m_iUsedSize += size;
			return 0;
		}
		public int addChar(int aiType, sbyte acVal)
		{
			return this.addByteArray(aiType, new byte[]
			{
				(byte)acVal
			}, 1);
		}
		public int addByte(int aiType, byte acVal)
		{
			return this.addByteArray(aiType, new byte[]
			{
				acVal
			}, 1);
		}
		public int AddInt(int aiType, int aiVal)
		{
			return this.addByteArray(aiType, TLVUtil.int2byte(aiVal, 4, this.m_eDefaultMode == 0), 4);
		}
		public int AddBool(int aiType, bool abVal)
		{
			if (!abVal)
			{
				return this.AddInt(aiType, 0);
			}
			return this.AddInt(aiType, 1);
		}
		public int AddLong(int aiType, long allVal, bool abNetOrder = true)
		{
			byte[] bytes = BitConverter.GetBytes(allVal);
			if (this.m_eDefaultMode == 0)
			{
				return this.addByteArray(aiType, bytes, bytes.Length);
			}
			int num = bytes.Length / 2;
			for (int i = 0; i < num; i++)
			{
				byte b = bytes[i];
				bytes[i] = bytes[bytes.Length - i];
				bytes[bytes.Length - i] = b;
			}
			return this.addByteArray(aiType, bytes, bytes.Length);
		}
		public int AddWord(int aiType, int ahVal)
		{
			return this.addByteArray(aiType, new byte[]
			{
				(byte)(ahVal & 255),
				(byte)(ahVal >> 8 & 255)
			}, 2);
		}
		public int addDWord(int aiType, long awVal)
		{
			byte[] array = new byte[4];
			byte[] array2 = TLVTypeTransform.longToByteArrayLH(awVal);
			Buffer.BlockCopy(array2, 0, array, 0, 4);
			return this.addByteArray(aiType, array, 4);
		}
		public int addNestedTLV(int aiType, TLVPack apoVal)
		{
			if (0 > this.ensureSpace(aiType, apoVal.m_iUsedSize))
			{
				return -2;
			}
			int iUsedSize = this.m_iUsedSize;
			if (this.m_eDefaultMode == 0)
			{
				Buffer.BlockCopy(TLVUtil.int2byte(aiType, 4, true), 0, this.m_pcBuf, this.m_iUsedSize, 4);
				this.m_iUsedSize += 4;
				Buffer.BlockCopy(TLVUtil.int2byte(apoVal.m_iUsedSize, 4, true), 0, this.m_pcBuf, this.m_iUsedSize, 4);
				this.m_iUsedSize += 4;
			}
			else
			{
				if (this.m_eDefaultMode != 1)
				{
					return -8;
				}
				int num = TLVUtil.EncodeVByte32(aiType, this.m_pcBuf, this.m_iUsedSize);
				this.m_iUsedSize += num;
				int num2 = TLVUtil.EncodeVByte32(apoVal.m_iUsedSize, this.m_pcBuf, this.m_iUsedSize);
				this.m_iUsedSize += num2;
			}
			if (apoVal.m_iUsedSize > 0)
			{
				int num3 = 0;
				num3 = this.m_iAllocSize - this.m_iUsedSize;
				int num4 = apoVal.CopyTo(this.m_pcBuf, this.m_iUsedSize, ref num3);
				if (num4 != 0)
				{
					this.m_iUsedSize = iUsedSize;
					return num4;
				}
			}
			this.m_iUsedSize += apoVal.m_iUsedSize;
			return 0;
		}
		public int getByteArray(int aiType, ref byte[] apoVal)
		{
			if (this.m_iUsedSize == this.m_ptHeader.getSize())
			{
				return -6;
			}
			TLVPack.FixedSizeTLVBody fixedSizeTLVBody = new TLVPack.FixedSizeTLVBody();
			TLVPack.tVariableSizeTLVItem tVariableSizeTLVItem = new TLVPack.tVariableSizeTLVItem();
			TLVPack.TLVBody tLVBody;
			if (this.m_eDefaultMode == 0)
			{
				tLVBody = fixedSizeTLVBody;
			}
			else
			{
				if (this.m_eDefaultMode != 1)
				{
					return -8;
				}
				tLVBody = tVariableSizeTLVItem;
			}
			if (!tLVBody.MapTo(this.m_pcBuf, this.m_iUsedSize, this.m_ptHeader.getSize()))
			{
				return -5;
			}
			while (tLVBody.iType != aiType && tLVBody.iNextOffset != 0)
			{
				if (!tLVBody.MapTo(this.m_pcBuf, this.m_iUsedSize, tLVBody.iNextOffset))
				{
					return -5;
				}
			}
			if (tLVBody.iType != aiType)
			{
				return -6;
			}
			apoVal = new byte[tLVBody.iLength];
			Buffer.BlockCopy(tLVBody.pcValPtr, tLVBody.pcValPtrOffset, apoVal, 0, tLVBody.iLength);
			return 0;
		}
		public int GetByte(int aiType, ref byte apcVal)
		{
			byte[] array = null;
			int byteArray = this.getByteArray(aiType, ref array);
			apcVal = array[0];
			return byteArray;
		}
		public int GetBool(int aiType, ref bool apbVal)
		{
			byte[] byte_ = null;
			int byteArray = this.getByteArray(aiType, ref byte_);
			if (byteArray < 0)
			{
				return byteArray;
			}
			if (TLVUtil.byte2int(byte_, 0, 4, this.m_eDefaultMode == 0) == 0)
			{
				apbVal = false;
			}
			else
			{
				apbVal = true;
			}
			return 0;
		}
		public int GetShort(int aiType, ref short aphVal)
		{
			byte[] array = null;
			int byteArray = this.getByteArray(aiType, ref array);
			if (byteArray < 0)
			{
				return byteArray;
			}
			if (this.m_eDefaultMode == 0)
			{
				aphVal = (short)((int)((short)(array[0] & 255)) << 8 | (int)(array[1] & 255));
			}
			else
			{
				aphVal = (short)((int)((short)(array[1] & 255)) << 8 | (int)(array[0] & 255));
			}
			return 0;
		}
		public int GetWord(int aiType, ref int apwVal)
		{
			byte[] byte_ = null;
			int byteArray = this.getByteArray(aiType, ref byte_);
			if (byteArray < 0)
			{
				return byteArray;
			}
			apwVal = TLVUtil.byte2int(byte_, 0, 4, this.m_eDefaultMode == 0);
			return 0;
		}
		public int GetInt(int aiType, ref int apiVal)
		{
			byte[] byte_ = null;
			int byteArray = this.getByteArray(aiType, ref byte_);
			if (byteArray < 0)
			{
				return byteArray;
			}
			apiVal = TLVUtil.byte2int(byte_, 0, 4, this.m_eDefaultMode == 0);
			return 0;
		}
		public int GetUInt(int aiType, ref uint apuVal)
		{
			byte[] array = null;
			int byteArray = this.getByteArray(aiType, ref array);
			if (byteArray < 0)
			{
				return byteArray;
			}
			if (this.m_eDefaultMode == 0)
			{
				apuVal = (uint)((int)(array[0] & 255) << 24 | (int)(array[1] & 255) << 16 | (int)(array[2] & 255) << 8 | (int)(array[3] & 255));
			}
			else
			{
				apuVal = (uint)((int)(array[3] & 255) << 24 | (int)(array[2] & 255) << 16 | (int)(array[1] & 255) << 8 | (int)(array[0] & 255));
			}
			return 0;
		}
		public int GetLong(int aiType, ref int apuVal)
		{
			return this.GetInt(aiType, ref apuVal);
		}
		public int GetDWord(int aiType, ref uint apuVal)
		{
			return this.GetUInt(aiType, ref apuVal);
		}
		public int getLongLong(int aiType, ref long apuVal)
		{
			byte[] array = null;
			int byteArray = this.getByteArray(aiType, ref array);
			if (byteArray < 0)
			{
				return byteArray;
			}
			if (this.m_eDefaultMode == 0)
			{
				apuVal = ((long)(array[0] & 255) << 56 | (long)(array[1] & 255) << 48 | (long)(array[2] & 255) << 40 | (long)(array[3] & 255) << 32 | (long)(array[4] & 255) << 24 | (long)((ulong)((ulong)(array[5] & 255) << 16)) | (long)((ulong)((ulong)(array[6] & 255) << 8)) | (long)((ulong)(array[7] & 255)));
			}
			else
			{
				apuVal = ((long)(array[7] & 255) << 56 | (long)(array[6] & 255) << 48 | (long)(array[5] & 255) << 40 | (long)(array[4] & 255) << 32 | (long)(array[3] & 255) << 24 | (long)((ulong)((ulong)(array[2] & 255) << 16)) | (long)((ulong)((ulong)(array[1] & 255) << 8)) | (long)((ulong)(array[0] & 255)));
			}
			return 0;
		}
		public int getNestedTLVBuf(int aiType, TLVPack appoVal)
		{
			if (appoVal == null)
			{
				return -4;
			}
			if (this.m_iUsedSize == this.m_ptHeader.getSize())
			{
				return -6;
			}
			TLVPack.FixedSizeTLVBody fixedSizeTLVBody = new TLVPack.FixedSizeTLVBody();
			TLVPack.tVariableSizeTLVItem tVariableSizeTLVItem = new TLVPack.tVariableSizeTLVItem();
			TLVPack.TLVBody tLVBody;
			if (this.m_eDefaultMode == 0)
			{
				tLVBody = fixedSizeTLVBody;
			}
			else
			{
				if (this.m_eDefaultMode != 1)
				{
					return -8;
				}
				tLVBody = tVariableSizeTLVItem;
			}
			if (!tLVBody.MapTo(this.m_pcBuf, this.m_iUsedSize, this.m_ptHeader.getSize()))
			{
				return -5;
			}
			while (tLVBody.iType != aiType && tLVBody.iNextOffset != 0)
			{
				if (!tLVBody.MapTo(this.m_pcBuf, this.m_iUsedSize, tLVBody.iNextOffset))
				{
					return -5;
				}
			}
			if (tLVBody.iType != aiType)
			{
				return -6;
			}
			if (tLVBody.iLength > 0 && 0 > appoVal.CopyFrom(tLVBody.pcValPtr, tLVBody.iLength, tLVBody.pcValPtrOffset))
			{
				appoVal.m_pcBuf = null;
				appoVal.m_ptHeader.h_pcBuf = null;
				return -5;
			}
			return 0;
		}
		public int SizeNumber(int aiType, int aiSizeOfType, int eTLVMode)
		{
			if (eTLVMode == 0)
			{
				return 8 + aiSizeOfType;
			}
			if (eTLVMode == 1)
			{
				byte[] apcBuffer = new byte[6];
				int num = TLVUtil.EncodeVByte32(aiType, apcBuffer, 0);
				int num2 = TLVUtil.EncodeVByte32(aiSizeOfType, apcBuffer, 0);
				return num + num2 + aiSizeOfType;
			}
			return 2147483647;
		}
		public int SizeChar(int aiType, int eTLVMode)
		{
			return this.SizeNumber(aiType, 2, eTLVMode);
		}
		public int SizeChar(int aiType)
		{
			return this.SizeNumber(aiType, 2, 0);
		}
		public int SizeByte(int aiType)
		{
			return this.SizeNumber(aiType, 4, 0);
		}
		public int SizeByte(int aiType, int eTLVMode)
		{
			return this.SizeNumber(aiType, 4, eTLVMode);
		}
		public int SizeShort(int aiType)
		{
			return this.SizeNumber(aiType, 2, 0);
		}
		public int SizeShort(int aiType, int eTLVMode)
		{
			return this.SizeNumber(aiType, 2, eTLVMode);
		}
		public int SizeWord(int aiType)
		{
			return this.SizeNumber(aiType, 2, 0);
		}
		public int SizeWord(int aiType, int eTLVMode)
		{
			return this.SizeNumber(aiType, 2, eTLVMode);
		}
		public int izeInt(int aiType)
		{
			return this.SizeNumber(aiType, 4, 0);
		}
		public int izeInt(int aiType, int eTLVMode)
		{
			return this.SizeNumber(aiType, 4, eTLVMode);
		}
		public int SizeUInt(int aiType)
		{
			return this.SizeNumber(aiType, 4, 0);
		}
		public int SizeUInt(int aiType, int eTLVMode)
		{
			return this.SizeNumber(aiType, 4, eTLVMode);
		}
		public int SizeLong(int aiType)
		{
			return this.SizeNumber(aiType, 4, 0);
		}
		public int SizeLong(int aiType, int eTLVMode)
		{
			return this.SizeNumber(aiType, 4, eTLVMode);
		}
		public int SizeDWord(int aiType)
		{
			return this.SizeNumber(aiType, 4, 0);
		}
		public int SizeDWord(int aiType, int eTLVMode)
		{
			return this.SizeNumber(aiType, 4, eTLVMode);
		}
		public int SizeLongLong(int aiType)
		{
			return this.SizeNumber(aiType, 8, 0);
		}
		public int SizeLongLong(int aiType, int eTLVMode)
		{
			return this.SizeNumber(aiType, 8, eTLVMode);
		}
		public int SizeULongLong(int aiType)
		{
			return this.SizeNumber(aiType, 8, 0);
		}
		public int SizeULongLong(int aiType, int eTLVMode)
		{
			return this.SizeNumber(aiType, 8, eTLVMode);
		}
		public int SizeBool(int aiType)
		{
			return this.SizeNumber(aiType, 4, 0);
		}
		public int SizeBool(int aiType, int eTLVMode)
		{
			return this.SizeNumber(aiType, 4, eTLVMode);
		}
		public int SizeBuf(int aiType, int aiLen)
		{
			return this.SizeNumber(aiType, aiLen, 0);
		}
		public int SizeBuf(int aiType, int aiLen, int eTLVMode)
		{
			return this.SizeNumber(aiType, aiLen, eTLVMode);
		}
		public int SizeTLV(int aiType, TLVPack apoVal)
		{
			return this.SizeNumber(aiType, apoVal.m_iUsedSize, 0);
		}
		public int SizeTLV(int aiType, TLVPack apoVal, int eTLVMode)
		{
			return this.SizeNumber(aiType, apoVal.m_iUsedSize, eTLVMode);
		}
		public int SizeType(int aiType, int eTLVMode)
		{
			if (eTLVMode == 0)
			{
				return 4;
			}
			if (eTLVMode != 1)
			{
				return 2147483647;
			}
			if (aiType == 0)
			{
				return 2147483647;
			}
			byte[] apcBuffer = new byte[6];
			return TLVUtil.EncodeVByte32(aiType, apcBuffer, 0);
		}
		public int SizeLength(int aiLen, int eTLVMode)
		{
			if (eTLVMode == 0)
			{
				return 4;
			}
			if (eTLVMode != 1)
			{
				return 2147483647;
			}
			if (aiLen == -1)
			{
				return 2147483647;
			}
			byte[] apcBuffer = new byte[6];
			return TLVUtil.EncodeVByte32(aiLen, apcBuffer, 0);
		}
		public int SizeHeader()
		{
			return this.m_ptHeader.getSize();
		}
	}
}
